import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:loxen/api/app_upgrade.dart';
import 'package:loxen/dialog/toast.dart';
import 'package:loxen/utils/config.dart';
import 'package:loxen/utils/save_util.dart';
import 'package:loxen/view/splash_page.dart';
import 'package:loxen/viewmode/master_index_viewmodel.dart';
import 'package:loxen/viewmode/study_index_viewmodel.dart';
import 'package:loxen/routes/routes.dart';
import 'package:loxen/view/master/master_index_widget.dart';
import 'package:loxen/view/material/material_index_widget.dart';
import 'package:loxen/viewmode/login_view_model.dart';
import 'package:provider/provider.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:loxen/utils/string_res.dart';
import 'package:loxen/view/inspiration/inspiration_list_widget.dart';
import 'package:loxen/viewmode/inspiration_list_viewmodel.dart';
import 'package:loxen/viewmode/material_index_viewmodel.dart';
import 'package:loxen/view/study/study_widget.dart';
import 'package:loxen/view/mine/mine_page_widget.dart';
import 'package:loxen/viewmode/mine_page_viewmodel.dart';


class HomePageWidget extends StatelessWidget {

  final defaultColor = Color(0xFF999999);
  final activeColor = Color(0xFF0CABCB);

  final ValueNotifier<int> _bottomIndex = ValueNotifier(0);
  final InspirationListViewModel _inspirationListViewModel = InspirationListViewModel();
  final MaterialIndexViewModel _materialIndexViewModel = MaterialIndexViewModel();
  final MinePageViewModel _minePageViewModel = MinePageViewModel();
  final StudyViewModel _studyViewModel = StudyViewModel();
  final MasterIndexViewModel _masterIndexViewModel = MasterIndexViewModel();
  final List<Widget> list = [
    const InspirationListWidget(),
    const MaterialIndexWidget(),
    const StudyIndexWidget(),
    const MasterIndexWidget(),
    const MinePageWidget(),
  ];

  @override
  Widget build(BuildContext context) {
    openSplash(context);
    AppUpGrade.upGrade(context);
    _inspirationListViewModel.loadData();
    _materialIndexViewModel.loadData();
    _studyViewModel.loadData();
    DateTime lastPopTime;
    double icon = 40.w;
    return MultiProvider(
      providers: [
        ChangeNotifierProvider<InspirationListViewModel>.value(
          value: _inspirationListViewModel,
        ),
        ChangeNotifierProvider<MaterialIndexViewModel>.value(
          value: _materialIndexViewModel,
        ),
        ChangeNotifierProvider<MinePageViewModel>.value(
          value: _minePageViewModel,
        ),
        ChangeNotifierProvider<StudyViewModel>.value(
          value: _studyViewModel,
        ),
        ChangeNotifierProvider<MasterIndexViewModel>.value(value: _masterIndexViewModel),
        ValueListenableProvider.value(value: _bottomIndex),
      ],
      child: WillPopScope(
        onWillPop: () async {
          if (lastPopTime == null ||
              DateTime.now().difference(lastPopTime) > Duration(seconds: 2)) {
            lastPopTime = DateTime.now();
            Toast.show('再按一次退出', context);
          } else {
            lastPopTime = DateTime.now();
            await SystemChannels.platform.invokeMethod('SystemNavigator.pop');
          }
          return false;
        },
        child: Scaffold(
          body: Builder(builder: (context) {
            return IndexedStack(
              index: context.watch<int>(),
              children: list,
            );
          }),
          bottomNavigationBar: Builder(
              builder: (context) => BottomNavigationBar(
                currentIndex: context.watch<int>(),
                onTap: (index) {
                  if (index == 3) {
                    if (context.read<LoginViewModel>().isLogin()) {
                      Navigator.of(context).pushNamed(Routes.login_page);
                      return;
                    }
                    _masterIndexViewModel.loadData();
                  } else if (index == 4) {
                    _minePageViewModel.loadData();
                  }
                  _bottomIndex.value = index;
                },
                selectedFontSize: 22.sp,
                unselectedFontSize: 22.sp,
                type: BottomNavigationBarType.fixed,
                items: <BottomNavigationBarItem>[
                  BottomNavigationBarItem(
                      icon: Image.asset(
                        'images/tab0_default.png',
                        fit: BoxFit.cover,
                        width: icon,
                        height: icon,
                      ),
                      activeIcon: Image.asset(
                        'images/tab0_active.png',
                        fit: BoxFit.cover,
                        width: icon,
                        height: icon,
                      ),
                      title: Text(StringResources.shou_ye,
                          style: TextStyle(
                              color: context.watch<int>() != 0 ? defaultColor : activeColor))),
                  BottomNavigationBarItem(
                      icon: Image.asset(
                        'images/tab1_default.png',
                        fit: BoxFit.cover,
                        width: icon,
                        height: icon,
                      ),
                      activeIcon: Image.asset(
                        'images/tab1_active.png',
                        fit: BoxFit.cover,
                        width: icon,
                        height: icon,
                      ),
                      title: Text(StringResources.su_cai,
                          style: TextStyle(
                              color: context.watch<int>() != 1 ? defaultColor : activeColor))),
                  BottomNavigationBarItem(
                      icon: Image.asset(
                        'images/tab2_default.png',
                        fit: BoxFit.cover,
                        width: icon,
                        height: icon,
                      ),
                      activeIcon: Image.asset(
                        'images/tab2_active.png',
                        fit: BoxFit.cover,
                        width: icon,
                        height: icon,
                      ),
                      title: Text(StringResources.xue_xi,
                          style: TextStyle(
                              color: context.watch<int>() != 2 ? defaultColor : activeColor))),
                  BottomNavigationBarItem(
                    icon: Image.asset(
                      'images/tab3_default.png',
                      fit: BoxFit.cover,
                      width: icon,
                      height: icon,
                    ),
                    activeIcon: Image.asset(
                      'images/tab3_active.png',
                      fit: BoxFit.cover,
                      width: icon,
                      height: icon,
                    ),
                    title: Text(
                      StringResources.da_shi_zhuo,
                      style: TextStyle(
                          color: context.watch<int>() != 3 ? defaultColor : activeColor),
                    ),
                  ),
                  BottomNavigationBarItem(
                      icon: Image.asset(
                        'images/tab4_default.png',
                        fit: BoxFit.cover,
                        width: icon,
                        height: icon,
                      ),
                      activeIcon: Image.asset(
                        'images/tab4_active.png',
                        fit: BoxFit.cover,
                        width: icon,
                        height: icon,
                      ),
                      title: Text(StringResources.wo_de,
                          style: TextStyle(
                              color: context.watch<int>() != 4 ? defaultColor : activeColor)))
                ],
              )),
        ),
      ),
    );
  }

  void openSplash(BuildContext context) async {
    var data = await SharedPreferencesUtil.getData(Constants.FIRST);
    if (data == null) {
      Navigator.push(context, MaterialPageRoute(builder: (_) => SplashPage()));
      SharedPreferencesUtil.putData(Constants.FIRST, true);
    }
  }
}




